package source;

/*
 * Author: Aqeel Kazmi
 * Email: aqeelhkazmi@gmail.com
*/

import javax.media.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import java.awt.*;
import java.net.URL;

public class ControlListen implements ControllerListener
{	
	MediaPlayerFrame mpf = null;
	
	public ControlListen(MediaPlayerFrame mpf)
	{
		this.mpf = mpf;		
	}
	
	public void controllerUpdate(ControllerEvent ce)
	{
		if(ce instanceof EndOfMediaEvent)
		{
			if(mpf.loops==true)
			{
				mpf.sf.nextSong();
			}
			else
			{
				mpf.getPlayer().stop();
			}
		} 
		
		if(ce instanceof StartEvent)
		{
			URL url1 = getClass().getResource("images/pause.gif");
			Icon openIcon = new ImageIcon(url1);
			//JButton b1 = new JButton("\nPause", openIcon);
			mpf.controlPanel.play.setIcon(openIcon);
			mpf.controlPanel.play.setText(" Pause");
			mpf.controlPanel.play.setActionCommand("Pause");
		}
		
		if(ce instanceof StopEvent)
		{
			URL url1 = getClass().getResource("images/play.gif");
			Icon openIcon = new ImageIcon(url1);
			mpf.controlPanel.play.setIcon(openIcon);
			mpf.controlPanel.play.setText(" Play");
			mpf.controlPanel.play.setActionCommand("Play");
		}
		
		if(ce instanceof RealizeCompleteEvent)
		{			
		}
		
		if(ce instanceof PrefetchCompleteEvent)
		{		
			mpf.displayAndControl.removeAll();
			mpf.displayAndControl.setFocusable(true);
			mpf.displayAndControl.addKeyListener(new KeysListen(mpf));
			mpf.fullScreen = true;

			if(mpf.getPlayer()!=null)      
        	{	
    			mpf.controlButtons.removeAll();
    			mpf.controlButtons.setFocusable(true);
    			mpf.controlButtons.addKeyListener(new KeysListen(mpf));
    			mpf.controlButtons.add(mpf.controlPanel.cnp(),BorderLayout.SOUTH);
    			mpf.controlButtons.add(mpf.controlPanel.controllerBar.cpanel(),BorderLayout.NORTH);
    			mpf.displayAndControl.add(mpf.controlButtons,BorderLayout.SOUTH);
    			
				//IF Player is created then check for its visual components
        		if(mpf.getPlayer().getVisualComponent()!=null)        		
				{
				mpf.getPlayer().getVisualComponent().addKeyListener(new KeysListen(mpf));		
				mpf.getPlayer().getVisualComponent().setFocusable(true);
				if(mpf.fullScreenFrame.isVisible()==false)
				{	//Place buttons if fullscreen is off
		        mpf.displayAndControl.add(mpf.getPlayer().getVisualComponent(),BorderLayout.CENTER);
				}
				else
				{	//If fullscreen is on then make its visual components visible to fullScreenFrame
				mpf.fullScreenFrame.removeAll();
				mpf.fullScreenFrame.setFocusable(true);
				mpf.fullScreenFrame.setVisible(false);
				mpf.fullScreenFrame.add(mpf.getPlayer().getVisualComponent(),BorderLayout.CENTER);
				mpf.fullScreenFrame.setVisible(true);
				}
				}
				else		            	
				{
				if(mpf.fullScreenFrame.isVisible()==true)
				{	//If full screen is visible (might b an audio clip) so set background
				mpf.fullScreenFrame.removeAll();
				mpf.fullScreenFrame.setFocusable(true);
				mpf.fullScreenFrame.setVisible(false);
				mpf.fullScreenFrame.add(mpf.backGround,BorderLayout.CENTER);
				mpf.fullScreenFrame.setVisible(true);
				}
			
				else
					mpf.displayAndControl.add(mpf.backGround,BorderLayout.CENTER);	              
	    		}
				  	
			}    		   		
    		
    			mpf.getPlayer().start();		
		 }
		
	}
}